
/**
 * Created by wxnacy on 2015/8/12.
 */
$(function() {

    //加载table数据
    initPage();
    submitForm();
    //createTableData(1);
    //翻页按钮公共效果
   // pageUtil();
});


function initPage(){
    var dataUrl = $('#listTable').attr('data-url');
    //console.log(dataUrl)
    //翻页数据
    $('#ul-page').find('a').each(function(e){
        $(this).click(function(e){
            var page = $(this).attr('page');
            $(this).attr('href',dataUrl+'?page='+page);
        });
    });
    //跳转页码
    $('a:contains("跳转")').click(function(){
        var dataUrl = $('#listTable').attr('data-url');
        var page = $('input[name="page"]').val();
        $(this).attr('href',dataUrl+'?page='+page);
    });
    //跳转页码
    $('button:contains("返回")').click(function(){
        history.go(-1);
    });
    //button按钮的跳转
    $('button[data-url]').click(function(){
       var dataUrl = $(this).attr('data-url');
        window.location.href = dataUrl;
    });
    //button按钮的异步跳转
    $('button[ajax-url]').click(function(){
        var ajaxUrl = $(this).attr('ajax-url');
        $.ajax({
            type: "get",
            dataType: "json",
            url: ajaxUrl,
            error: function () {
                alert('error');
            },
            success: function (data) {
                if(data.success){
                    swal({
                        title: data.msg,
                        text: '点击返回',
                        type:'success'
                    },function(){
                        location.reload();
                    });
                }else{
                    swal('操作失败', data.msg, 'error');
                }
            }
        });

    });
}

function submitForm(){
   //$('button[name=submit]').click(function(){
       $('form[name="ajaxForm"]').submit(function() {
           // 提交表单
           $(this).ajaxSubmit(function(data){
               console.log(data.success)
               if(data.success){
                   swal({
                       title: data.msg,
                       text: '点击返回',
                       type:'success'
                   },function(){
                       window.location.href=document.referrer;
                   });
               }else{
                   swal('操作失败', data.msg, 'error');
               }
               //alert(data.msg);
           });
           // 为了防止普通浏览器进行表单提交和产生页面导航（防止页面刷新？）返回false
           return false;
       });
   //})
}


/**
 * 创建表格数据
 */
function createTableData(page_){

    if($table){
        var url = 'http://test.zhengzai.tv'+ $table.attr('data-url')+'?page='+page_;//+"?name="+$('#channelName').val();
        console.log(url);
        $.ajax({
            type: "get",
            dataType: "json",
            url: url,
            error : function() {
                alert('error');
            },
            success : function(data) {
                var total = data.total;//总条数
                var pageSize = data.pageSize;//每页个数
                var totalPage = parseInt(total/pageSize);//总页数
                if(totalPage*pageSize<total){
                    totalPage = totalPage+1;
                }
                var page = data.page;//当前页码
                var list = data.rows;//数据
                var totalHtml = '';
                for(var i=0;i<list.length;i++){
                    var data_ = list[i];
                    var channelId = data_.channelId;
                    var displayOrder = data_.displayOrder;
                    var name = data_.name;

                    var html = '<tr><td>'+channelId+'</td><td>'+name+'</td><td>'+displayOrder+'</td><td>'
                        html = html+'    <button type="button" class="btn btn-info btn-sm btn-icon mr5" onclick="javascript:location.reload()">'
                        html = html+ '   <i class="fa fa-edit"></i>'
                        html = html+'    <span>修改</span>'
                        html = html +'    </button>'
                        html = html + '    </td> </tr>';
                    totalHtml = totalHtml + html;
                }
                $table.find('tbody').html(totalHtml);

                //翻页加载数据
                var $page_ul = $('#ul-page');
                $page_ul.attr('now-page',page);
                //$page_ul.find('a#first-page').attr('page',1);
                $page_ul.find('a#first-page').attr('page',1);
                $page_ul.find('a#next-page').attr('page',page+1);
                $page_ul.find('a#last-page').attr('page',totalPage);

                $('#page-total').find('span').html(page+'/'+totalPage+'页');
                $('#total-rows').find('span').html('总共：'+total+'条');

                //设置翻页的显示与否
                $('#ul-page').find('a').each(function(e) {
                    var this_page = $(this).attr('page');
                    if (page_ == this_page) {
                        $(this).parent().addClass('disabled');
                    }
                });
            }
        });
    }

}


/**
 * 翻页的公共效果
 */
function pageUtil(){
    var $page_ul = $('#ul-page');
    $page_ul.find('a').each(function(e){
        var page_  = $(this).attr('page');
        $(this).click(function(e){

            createTableData(page_);
        });
    });
}